package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author syf
 * @version 1.0
 * @date 2019/07/31 20:50
 * @Description TODO:品牌控制器
 */
@RefreshScope //自定义的配置文件想让消息总线刷新
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Value("${sms.addr}")
    private String addr;

    @Autowired
    private BrandService brandService;

    @GetMapping("/getAddr")
    public String getAddr(){
        return addr;
    }

    /**
     * 获取品牌列表信息
     * @param page
     * @param rows
     * @param key
     * @param sortBy
     * @param desc
     * @return
     */
    //BrandDTO防止把表结构传出去
    @GetMapping("/page")
    public ResponseEntity<PageResult<BrandDTO>> pageQuery(
            @RequestParam(value = "page", defaultValue = "1") Integer page, //当前页码
            @RequestParam(value = "rows", defaultValue = "5") Integer rows, //每页显示条数
            @RequestParam(value = "key", required = false) String key, //搜索关键词
            @RequestParam(value = "sortBy", required = false) String sortBy, //排序字段
            @RequestParam(value = "desc", defaultValue = "false") Boolean desc //是否降序
                                                                    ){
        return ResponseEntity.ok(brandService.pageQuery(key,page,rows,sortBy,desc));
    }

    /**
     * 新增品牌
     * @param brandDTO
     * @param cids
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids ){
        brandService.saveBrand(brandDTO,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 商品列表
     * 根据id查询品牌信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO>  findBrandById(@PathVariable("id") Long id){
        return ResponseEntity.ok(brandService.findBrandById(id));
    }

    /**
     * 根据分类id查询品牌信息
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> findByCategory(@RequestParam("id")Long id){
        return ResponseEntity.ok(brandService.findByCategory(id));
    }

    //根据品牌id批量查询品牌
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> findByBrand(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(brandService.findByBrand(ids));
    }
}
